package com.example.gustavo.hello.utils;

import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.CharacterStyle;
import android.text.style.ClickableSpan;
import android.util.Log;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by gustavo on 2015/4/8.
 */
public class SpanUtils {


    private static final String TAG = "Span";

    /**
     *
     * @param source
     * @param pattern
     * @param characterStyles   ClickableSpan有问题,CharacterStyle.wrap(style)
     * @return
     */
    public static SpannableStringBuilder setSpanText(String source, Pattern pattern, CharacterStyle... characterStyles){


        SpannableStringBuilder ssb = new SpannableStringBuilder(source);

        Matcher matcher = pattern.matcher(source);

        while (matcher.find()){

            int start = matcher.start();
            int end = matcher.end();
            Log.w(TAG,"start= "+start + " end= "+ end);

            for (CharacterStyle style : characterStyles) {


                ssb.setSpan(CharacterStyle.wrap(style), start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            }

        }

        return ssb;

    }




}
